package com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.entity.Table09421;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import java.util.ArrayList;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.entity.Table18109;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.service.ITable18109Service;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.dto.Table09421ListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.mapper.Table09421Mapper;
import com.xjrsoft.module.customerTwo.companyManage.calculationComponentExample.service.ITable09421Service;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.core.tool.utils.Func;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2022-08-25
 */
@Service
@AllArgsConstructor
public class Table09421ServiceImpl extends BaseService<Table09421Mapper, Table09421> implements ITable09421Service {

	private ITable18109Service table18109Service;

	@Override
	public IPage<Table09421> getPageList(Table09421ListDto pageListDto) {
		Wrapper<Table09421> wrapper = Wrappers.<Table09421>query().lambda()
				.like(!StringUtil.isEmpty(pageListDto.getField1003043()), Table09421::getField1003043, pageListDto.getField1003043())
				.ge(!StringUtil.isEmpty(pageListDto.getField10180c9_Start()), Table09421::getField10180c9, pageListDto.getField10180c9_Start())
				.le(!StringUtil.isEmpty(pageListDto.getField10180c9_End()), Table09421::getField10180c9, pageListDto.getField10180c9_End())
				.eq(!StringUtil.isEmpty(pageListDto.getField1026662()), Table09421::getField1026662, pageListDto.getField1026662())
				.eq(!StringUtil.isEmpty(pageListDto.getField103aa2f()), Table09421::getField103aa2f, pageListDto.getField103aa2f())
				.eq(!StringUtil.isEmpty(pageListDto.getField104a36c()), Table09421::getField104a36c, pageListDto.getField104a36c());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addTable09421(Table09421 table09421, List<Table18109> table18109List) {
		String table09421Id = IdWorker.get32UUID();
		table09421.setId(table09421Id);
		table18109List.forEach(table18109 -> table18109.setTable09421Id(table09421Id));
		table18109Service.saveBatch(table18109List);
		boolean isSuccess = this.save(table09421);
		return isSuccess ? table09421Id : null;
	}

	@Override
	public boolean updateTable09421(String id, Table09421 table09421, List<Table18109> table18109List) {
		table09421.setId(id);
		// Table18109
		List<String> table18109IdList = new ArrayList<>();
		for (Table18109 table18109 : table18109List) {
			String table18109Id = table18109.getId();
			if (StringUtil.isEmpty(table18109Id)) {
				table18109.setTable09421Id(id);
			} else {
				table18109IdList.add(table18109Id);
			}
		}
		if (CollectionUtil.isNotEmpty(table18109IdList)) {
			table18109Service.remove(Wrappers.<Table18109>query().lambda()
					.notIn(Table18109::getId, table18109IdList)
					.eq(Table18109::getTable09421Id, id));
		}
		table18109Service.saveOrUpdateBatch(table18109List);
		return this.updateById(table09421);
	}

	public boolean delete(String ids) {
		List<String> idList = Func.toStrList(ids);
		if (this.removeByIds(idList)) {
			table18109Service.remove(Wrappers.<Table18109>query().lambda().in(Table18109::getTable09421Id, ids));
			return true;
		}
		return false;
	}

	public List<Table18109> getTable18109ByParentId(String parentId){
		Wrapper wrapper = Wrappers.<Table18109>query().lambda().eq(Table18109::getTable09421Id, parentId);
		return table18109Service.list(wrapper);
	}
}